<?php
/**
 * @author GhostWei
 * @date Sep 15, 2012
 */
class oauth_response_iweibo extends oauth_api_response {
	
	public function getAvatar() {
		$head = $this->get('head');
		return $head ? $head . '/180' : resource_config_avatar::noMedia();
	}
	
	public function getNickname() {
		return $this->get('nick');
	}
	
	public function getGender() {
		return $this->get('sex', 0);
	}
	
	public function getUid() {
		return $this->get('name', '');
	}
	
	public function getTweets() {
		$data = array();
		foreach($this->get('info', array()) AS $key => $row) {
			$data[] = $this->_contentRow($row);
		}
		return $data;
	}
	
	public function getTopicTweetsPaginator() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$page = abs($page);
		if($page == 0) $page = 1;

		$pageinfo = $this->get('pageinfo');
		$next = array(
				'page' => $page + 1,
				'pageflag' => '1',
				'pageinfo' => $pageinfo,
		);
		$prev = array(
				'page' => $page - 1,
				'pageflag' => '2',
				'pageinfo' => $pageinfo,
		);
		$pages = array();
		if($page <= 1) {
			$pages['Next'] = $next;
		} else {
			$pages['Previous'] = $prev;
			$pages['Next'] = $next;
		}
		$_page = $this->_pageSet();
		$_page->total = $this->get('totalnum', 0);
		$_page->page = $page;
		$_page->pages = $pages;
		return $_page;
	}
	
	public function getUserTweetsPaginator() {
		$ts = $this->getTweets();
		if(empty($ts)) {
			return $this->_pageSet();
		}
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$page = abs($page);
		if($page == 0) $page = 1;
		
		$last = $ts[count($ts) - 1];
		$next = array(
				'page' => $page + 1,
				'pageflag' => '1',
				'pagetime' => $last->timeline,
				'lastid' => $last->id,
		);
		$last = $ts[0];
		$prev = array(
				'page' => $page - 1,
				'pageflag' => '2',
				'pagetime' => $last->timeline,
				'lastid' => $last->id,
		);
		$pages = array();
		if($page <= 1) {
			$pages['Next'] = $next;
		} else {
			$pages['Previous'] = $prev;
			$pages['Next'] = $next;
		}
		
		$_page = $this->_pageSet();
		$_page->total = $this->get('totalnum', 0);
		$_page->page = $page;
		$_page->pages = $pages;
		return $_page;
	}
	
	private function _formatContent($text, $at) {	
		if(preg_match_all('/@([a-zA-Z]{1}[\w\-]+)/i', $text, $matches)) {
			foreach($matches[1] as $key => $match) {
				if(isset($at[$match])) {
					$text = str_replace($matches[0][$key], '[@' . $match . '|' . $at[$match] . ']', $text);
				} else {
					$text = str_replace($matches[0][$key], '[@' . $match . '|' . $match . ']', $text);
				}
			}
		}		
		return $text;
	}
	
	private function _contentRow($row) {
		$tweet = $this->_rowTweet();
		$tweet->author->id = isset($row->name) ? $row->name : '';
		$tweet->author->name = isset($row->nick) ? $row->nick : '';
		$tweet->author->location = isset($row->location) ? $row->location : '';
		$tweet->author->homepage = isset($row->url) ? $row->url : '';
		$tweet->author->avatar = isset($row->head) ? $row->head . '/180' : resource_config_avatar::noMedia();
			
		$tweet->id = isset($row->id) ? $row->id : time();
		$tweet->content = isset($row->origtext) ? $this->_formatContent($row->origtext, (array) $this->get('user', array())) : '';
		if(isset($row->source)) {
			$tweet->quote = $this->_contentRow($row->source);
		}
		$tweet->timeline = isset($row->timestamp) ? $row->timestamp : time();
		$tweet->hot = isset($row->mcount) ? $row->mcount : 0;
		if(isset($row->image)) {
			foreach((array) $row->image AS $img) {
				$tweet->images[] = array(
						'thumb' => $img . '/160',
						'middle' => $img . '/320',
						'original' => $img . '/2000',
				);
			}
		}
		$tweet->source = array('name' => null, 'url' => null);
		if(isset($row->from)) {
			$tweet->source['name'] = $row->from;
		}
		if(isset($row->fromurl)) {
			$tweet->source['url'] = trim($row->fromurl);
		}
		return $tweet;
	}
	
	public function getUser() {
		$user = $this->_rowAuthor();
		$user->id = $this->getUid();
		$user->name = $this->getNickname();
		$user->location = $this->get('location');
		$user->description = $this->get('introduction');
		$user->homepage = $this->get('homepage');
		$user->avatar = $this->getAvatar();
		$user->gender = $this->getGender();
		$user->tweets = $this->get('tweetnum', 0);
		$user->follows = $this->get('idolnum', 0);
		$user->following = $this->get('mutual_fans_num', 0);
		return $user;
	}
	
	public function getTopics() {
		$data = array();
		foreach($this->get('info', array()) AS $row) {
			$topic = $this->_rowTopic();
			$topic->name = $row->keywords;
			$topic->title = $row->keywords;
			$topic->hot = $row->tweetnum;
			$topic->description = $row->name;
			$data[] = $topic;
		}
		return $data;
	}
	
}
